import numpy as np
import fastremap
from common.ml_utils.metrics import compute_pri_and_voi


if __name__ == "__main__":
    # 16个点任取2个点，C(16,2) = 120
    # 在GT里：
    # 1个pixel pair在同一个label 0 有28对
    # 1个pixel pair在同一个label 1 有28对
    # 1个pixel pair在同一个label 共有28+28=56对
    # 1个pixel pair在不同的label 有64对
    # 在Pred里：
    # 1个pixel pair在同一个label 有120对
    # 1个pixel pair在不同的label 有0对
    # 所以，(56 / 120) * (120 / 120) + (64 / 120) * (0 / 120) = 0.4666667 + 0 = 0.4666667

    gt_labels = np.array([[1, 1, 2, 2], [1, 1, 2, 2], [1, 1, 2, 2], [1, 1, 2, 2]])

    pred_labels = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]])

    curRI, curVOI = compute_pri_and_voi(pred_labels, gt_labels)
    print("current RI is: {}".format(curRI))
    print("current VoI is: {}".format(curVOI))

    # 经测试，metric和label从1起，还是从0起，无关
    if not np.any(gt_labels == 0):
        # 判断是否有0元素，如果没有0元素，就从0来remap了
        gt_labels, _ = fastremap.renumber(gt_labels, start=0, in_place=False)
    if not np.any(pred_labels == 0):
        # 判断是否有0元素，如果没有0元素，就从0来remap了
        pred_labels, _ = fastremap.renumber(pred_labels, start=0, in_place=False)

    curRI, curVOI = compute_pri_and_voi(pred_labels, gt_labels)

    print("current RI is (label remap start from 0 if needed): {}".format(curRI))
    print("current VoI is (label remap start from 0 if needed): {}".format(curVOI))
